package com.gitee.liuzhi.demo.juc.thread;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author liuzhi
 * @Date 2023/9/6 20:39
 * @Version 1.0
 * @Description
 */
public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        for (int i = 0; i < 100; i++) {
            // 3.创建MyCallable实例,使用FutureTask封装创建MyCallable实例
            FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
            new Thread(futureTask).start();
            // 4.获取Callable的返回值
            System.out.println(futureTask.get());
        }
    }

    /**
     * 1.创建Callable接口的实现类MyCallable
     */
    static class MyCallable implements Callable<Integer> {
        // 2.重写call方法
        @Override
        public Integer call() {
            return new Random().nextInt(100) + 1;
        }
    }

}
